package com.jason.library.immersion.core

import android.annotation.SuppressLint
import android.app.Activity
import android.graphics.Color
import android.os.Build
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.FrameLayout
import androidx.annotation.RequiresApi
import com.jason.library.immersion.model.BarParams
import com.jason.library.immersion.model.Constants
import com.jason.library.immersion.utils.BarUtils
import com.jason.library.immersion.utils.OSUtils

/**
 * Description:
 *
 * @author chenbaoyang
 * @create 2021/1/11 16:51
 */


@SuppressLint("NewApi")
fun Activity.updateStatusBar(params: BarParams) {

    //防止系统栏隐藏时内容区域大小发生变化
    var uiFlags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE

    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && !OSUtils.isEMUI3_x()) {

        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)

        if (params.statusBarColor != -1 && params.statusBarColor != 0) {
            //设置状态栏颜色
            window.statusBarColor = params.statusBarColor

            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
            window.setBackgroundDrawable(null)


        }else if(params.resId > 0) {
            //设置背景图案
            val drawable = getDrawable(params.resId)
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
            window.navigationBarColor = Color.TRANSPARENT
            window.statusBarColor = Color.TRANSPARENT
            window.setBackgroundDrawable(drawable)
        }

    }else{
        setupStatusBarView(params);
    }
}

/**
 * 在android 4.4的系统给状态栏添加一层同样宽高的view
 */
@RequiresApi(Build.VERSION_CODES.KITKAT)
private fun Activity.setupStatusBarView(params: BarParams) {
    //透明状态栏
    window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
    //获取window的根部view
    val decorView = window.decorView as FrameLayout
    var statusBarView = decorView.findViewById<View>(Constants.IMMERSION_ID_STATUS_BAR_VIEW)
    if (statusBarView == null) {
        statusBarView = View(this)
        val statusBarHeight = BarUtils.getStatusBarHeight(this)
        val layoutParams = FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, statusBarHeight)
        layoutParams.gravity = Gravity.TOP

        statusBarView.layoutParams = layoutParams
        statusBarView.visibility = View.VISIBLE
        statusBarView.id = Constants.IMMERSION_ID_STATUS_BAR_VIEW
        decorView.addView(statusBarView)
    }

    statusBarView.setBackgroundColor(params.statusBarColor)

    if(params.resId > 0) {
        statusBarView.setBackgroundResource(params.resId)
    }
}

fun Activity.updateNavigationBar(params: BarParams) {

    window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION)

    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && !OSUtils.isEMUI3_x()) {
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)

        if (params.navigationBarColor != -1 && params.navigationBarColor != 0) {
            //设置状态栏颜色
            window.navigationBarColor = params.statusBarColor

            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
            window.setBackgroundDrawable(null)


        }else if(params.resId > 0) {
            //设置背景图案
            val drawable = getDrawable(params.resId)
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
            window.navigationBarColor = Color.TRANSPARENT
            window.statusBarColor = Color.TRANSPARENT
            window.setBackgroundDrawable(drawable)
        }

    }else{
        setupNavigationBarView(params)
    }
}

fun Activity.transparentBar(params: BarParams) {
    transparentNavigationBar(params)
    transparentStatusBar(params)
}

private fun Activity.transparentNavigationBar(params: BarParams) {

    if(params.transparentNavigationBarEnable) {

        //Activity全屏显示，但状态栏不会被隐藏覆盖，状态栏依然可见，Activity顶端布局部分会被状态栏遮住。
        /*uiFlags = uiFlags or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
        uiFlags = uiFlags or View.SYSTEM_UI_FLAG_VISIBLE
        uiFlags = uiFlags or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
        window.decorView.setSystemUiVisibility(uiFlags)*/
        //需要设置这个才能设置状态栏和导航栏颜色
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
        window.setBackgroundDrawable(null)
        //透明状态栏
        window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION)
        //window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION)
    }
}

private fun Activity.transparentStatusBar(params: BarParams) {
    if(params.transparentStatusBarEnable) {

        //Activity全屏显示，但状态栏不会被隐藏覆盖，状态栏依然可见，Activity顶端布局部分会被状态栏遮住。
        /*uiFlags = uiFlags or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
        uiFlags = uiFlags or View.SYSTEM_UI_FLAG_VISIBLE
        uiFlags = uiFlags or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
        window.decorView.setSystemUiVisibility(uiFlags)*/
        //需要设置这个才能设置状态栏和导航栏颜色
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
        window.setBackgroundDrawable(null)
        //透明状态栏
        window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
        //window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION)
    }
}

/**
 * 在的android 4.4的系统给导航栏加一层同样宽高的view
 */
private fun Activity.setupNavigationBarView(barParams: BarParams) {

    //透明导航栏，设置这个，如果有导航栏，底部布局会被导航栏遮住
    window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION)

    //获取window的根部view
    val decorView = window.decorView as FrameLayout
    var navigationBarView: View =
        decorView.findViewById(Constants.IMMERSION_ID_NAVIGATION_BAR_VIEW)
    if (navigationBarView == null) {
        navigationBarView = View(this)
        navigationBarView.id = Constants.IMMERSION_ID_NAVIGATION_BAR_VIEW
        decorView.addView(navigationBarView)
    }

    val layoutParams: FrameLayout.LayoutParams
    if (BarUtils.isNavigationAtBottom(this)) {
        layoutParams = FrameLayout.LayoutParams(
            FrameLayout.LayoutParams.MATCH_PARENT,
            BarUtils.getNavigationBarHeight(this)
        )
        layoutParams.gravity = Gravity.BOTTOM
    } else {
        layoutParams = FrameLayout.LayoutParams(
            BarUtils.getNavigationBarWidth(this),
            FrameLayout.LayoutParams.MATCH_PARENT
        )
        layoutParams.gravity = Gravity.END
    }
    navigationBarView.layoutParams = layoutParams
    if(barParams.resId > 0) {
        navigationBarView.setBackgroundResource(barParams.resId)
    }else if(barParams.navigationBarColor != -1){
        navigationBarView.setBackgroundColor(barParams.navigationBarColor)
    }


    if(BarUtils.getNavigationBarHeight(this) > 0 || OSUtils.isEMUI3_x()) {
        navigationBarView.visibility = View.VISIBLE
    }else{
        navigationBarView.visibility = View.GONE
    }

}

